#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "clientList.h"


struct clientfds *create_clientfds(void)
{
	struct clientfds *head = NULL;

	head = (struct clientfds *)malloc(sizeof(struct clientfds));
	if(head == NULL)
	{
		perror("malloc error");
		return NULL;
	}

	return head;
}

struct clientfds *insert(struct clientfds *head,struct clientfds data)
{
	struct clientfds *q = NULL;
	struct clientfds *p = head;

	while(p->next != NULL)
	{
		p = p->next;
	}

	q = (struct clientfds *)malloc(sizeof(struct clientfds));
	if(q == NULL)
	{
		perror("malloc error");
		return NULL;
	}
	memset(q,0,sizeof(struct clientfds));
	*q = data;
	p->next = q;
	q->next = NULL;

	return q;
}


int delete_by_fd(struct clientfds *head,int fd)
{
	struct clientfds *p = NULL;
	struct clientfds *q = NULL;

	q = head;
	p = head->next;

	while(p != NULL)
	{
		if(p->fd == fd)
		{
			q->next = p->next;
			free(p);
			return 1;
		}
		p = p->next;
		q = q->next;
	}

	return 0;
}



void free_clientfds(struct clientfds *head)
{
	struct clientfds *p = head;
	while(p != NULL)
	{
		free(p);
		p = p->next;
	}
}








